#include <47func.h>
int main(int argc, char *argv[])
{
    // ./4_server 192.168.118.128 1234
    ARGS_CHECK(argc,3);
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    // AF_INET ipv4 SOCK_STREAM TCP
    struct sockaddr_in serverAddr;//带in 是ipv4地址 带in6是ipv6地址 不带是通用地址
    serverAddr.sin_family = AF_INET;//ipv4
    serverAddr.sin_addr.s_addr = inet_addr(argv[1]);//把点分十进制的ip地址转换成32位大端存储的地址
    serverAddr.sin_port = htons(atoi(argv[2]));//把端口用大端存储
    int reuse = 1;// setsockopt函数中SO_REUSEADDR的选项
    int ret = setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse));
    ERROR_CHECK(ret,-1,"setsockopt");
    ret = bind(sockfd,(struct sockaddr *)&serverAddr,sizeof(serverAddr));
    ERROR_CHECK(ret,-1,"bind");
    ret = listen(sockfd,10);
    ERROR_CHECK(ret,-1,"listen");
    struct sockaddr_in clientAddr;
    socklen_t clientAddrSize = sizeof(clientAddr);//一定要赋予初值
    int epfd = epoll_create(1);
    struct epoll_event event;
    event.events = EPOLLIN;
    event.data.fd = sockfd;
    epoll_ctl(epfd,EPOLL_CTL_ADD,sockfd,&event);
    int netfd = -1;
    char buf[4096] = {0};
    struct epoll_event readyset[2];
    while(1){
        int readynum = epoll_wait(epfd,readyset,2,-1);
        for(int i = 0; i < readynum; ++i){
            if(readyset[i].data.fd == sockfd){
                // 有客户端 connect
                netfd = accept(sockfd,(struct sockaddr *)&clientAddr,&clientAddrSize);
                // sockfd专门用来建立连接 netfd负责和客户端的数据通信
                printf("addrlen = %d\n",clientAddrSize);
                printf("client sin_family = %d\n",clientAddr.sin_family);
                printf("client ip = %s\n", inet_ntoa(clientAddr.sin_addr));
                printf("client port =%d\n", ntohs(clientAddr.sin_port));
                // 修改监听的文件描述符
                event.events = EPOLLIN;
                event.data.fd = netfd;
                epoll_ctl(epfd,EPOLL_CTL_ADD,netfd,&event);
                event.events = EPOLLIN;
                event.data.fd = STDIN_FILENO;
                epoll_ctl(epfd,EPOLL_CTL_ADD,STDIN_FILENO,&event);
                event.events = EPOLLIN;
                event.data.fd = sockfd;
                epoll_ctl(epfd,EPOLL_CTL_DEL,sockfd,&event);
            }
            else if(netfd != -1 && readyset[i].data.fd == netfd){
                bzero(buf,sizeof(buf));
                ssize_t sret = recv(netfd,buf,sizeof(buf),0);
                if(sret == 0){
                    //对方断开连接
                    event.events = EPOLLIN;
                    event.data.fd = netfd;
                    epoll_ctl(epfd,EPOLL_CTL_DEL,netfd,&event);
                    close(netfd);
                    netfd = -1;
                    event.events = EPOLLIN;
                    event.data.fd = STDIN_FILENO;
                    epoll_ctl(epfd,EPOLL_CTL_DEL,STDIN_FILENO,&event);
                    event.events = EPOLLIN;
                    event.data.fd = sockfd;
                    epoll_ctl(epfd,EPOLL_CTL_ADD,sockfd,&event);
                    continue;
                }
                printf("buf = %s\n",buf);
            }
            else if(readyset[i].data.fd == STDIN_FILENO){
                bzero(buf,sizeof(buf));
                ssize_t sret = read(STDIN_FILENO,buf,sizeof(buf));
                if(sret == 0){
                    send(netfd,"nishigehaoren",13,0);
                    event.events = EPOLLIN;
                    event.data.fd = netfd;
                    epoll_ctl(epfd,EPOLL_CTL_DEL,netfd,&event);
                    close(netfd);
                    netfd = -1;
                    event.events = EPOLLIN;
                    event.data.fd = STDIN_FILENO;
                    epoll_ctl(epfd,EPOLL_CTL_DEL,STDIN_FILENO,&event);
                    event.events = EPOLLIN;
                    event.data.fd = sockfd;
                    epoll_ctl(epfd,EPOLL_CTL_ADD,sockfd,&event);
                    continue;
                }
                send(netfd,buf,strlen(buf),0);
            }
        }
    }
    return 0;
}

